package com.example.demo.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.topic.vo.TFansRecordVo;
import com.example.demo.user.entity.UServiceUser;
import com.example.demo.user.service.UServiceUserService;
import com.example.demo.user.vo.ServiceUserVo;
import com.example.demo.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 服务人员表; 前端控制器
 * </p>
 *
 * @author 涛
 * @since 2024-08-01
 */
@RestController
@RequestMapping("/uServiceUser")
@Tag(name = "uServiceUser", description = "服务人员相关接口")
public class UServiceUserController {

    @Autowired
    private UServiceUserService uServiceUserService;

    /**
     * 获取人员管理模块列表
     */
    @GetMapping("/getUserList")
    @Operation(summary = "获取服务人员接口", description = "对应原型中 人员管理-界面列表")
    public Result<?> getUserList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                 UServiceUser user){
        Page<UServiceUser> page = new Page<>(current,size);
        QueryWrapper<UServiceUser> queryWrapper = new QueryWrapper<>(user);
        return Result.ok(uServiceUserService.page(page,queryWrapper));
    }

    /**
     * 根据ID查询人员信息
     */
    @GetMapping("/getUserById")
    @Operation(summary = "根据id查询服务人员明细", description = "对应原型中 人员管理-查看")
    public Result<?> getUserById(Integer id){
        return Result.ok(uServiceUserService.getById(id));
    }

    /**
     * 修改人员信息
     */
    @PutMapping("/updateUser")
    @Operation(summary = "根据id修改服务人员明细", description = "对应原型中 人员管理-编辑、账号管理-添加删除管理员运营人员权限")
    @Transactional
    public Result<?> updateUser(@RequestBody UServiceUser user){
        return Result.ok(uServiceUserService.updateById(user));
    }

    /**
     * 新增人员信息
     */
    @PostMapping("/addUser")
    @Operation(summary = "新增服务人员明细", description = "对应原型中 人员管理-新增")
    public Result<?> addUser(@RequestBody UServiceUser user){
        user.setStatus("0");
        return Result.ok(uServiceUserService.save(user));
    }

    /**
     * 删除人员信息
     */
    @DeleteMapping("/deleteUserById")
    @Operation(summary = "删除服务人员", description = "对应原型中 人员管理-删除")
    @Transactional
    public Result<?> deleteUser(Integer id){
        return Result.ok(uServiceUserService.removeById(id));
    }

    /**
     * 获取账号管理模块列表
     */
    @GetMapping("/getUserList2")
    @Operation(summary = "获取账号管理模块列表", description = "对应原型中 账号管理-界面列表")
    public Result<?> getUserList2(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                 @RequestParam(value = "size", required = false, defaultValue = "10")Integer size,
                                  ServiceUserVo user){
        Page<ServiceUserVo> page = new Page<>(current,size);
        return uServiceUserService.getUserList(page,user);
    }

    /**
     * 移动端查看运营人员信息
     */
    @GetMapping("/getUserById2")
    @Operation(summary = "移动端查看运营人员信息", description = "移动端查看运营人员信息")
    public Result<?> getUserById2(Integer id,Integer uid){
        return uServiceUserService.getUserById2(id,uid);
    }

    /**
     * 获取推荐人员列表
     */
    @GetMapping("/getAdviceUserList")
    @Operation(summary = "获取推荐人员列表", description = "获取推荐人员列表")
    public Result<?> getAdviceUserList(@RequestParam(value = "current", required = false, defaultValue = "1")Integer current,
                                       @RequestParam(value = "size", required = false, defaultValue = "10")Integer size) {
        Page<TFansRecordVo> page = new Page<>(current, size);
        return uServiceUserService.getAdviceUserList(page);
    }

    @DeleteMapping("/batch/{ids}")
    @Operation(summary = "批量删除", description = "批量删除")
    public Result<Void> deleteDictType(@PathVariable String ids) {
        List<String> idArray = Arrays.asList(ids.split(","));
        uServiceUserService.removeBatchByIds(idArray);
        return Result.ok();
    }

    /**
     * 微信小程序登录绑定手机号返回用户信息
     */
    @GetMapping("/bindWxAccount")
    @Operation(summary = "微信小程序登录绑定手机号返回用户信息", description = "微信小程序登录绑定手机号返回用户信息")
    public Result<?> getUserList(UServiceUser user){
        QueryWrapper<UServiceUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("contact",user.getContact());
        UServiceUser uServiceUser = uServiceUserService.getOne(queryWrapper);
        if(uServiceUser == null){
            return Result.error(new Exception("该手机号未注册"));
        }
        uServiceUser.setWxopenid(user.getWxopenid());
        uServiceUserService.updateById(uServiceUser);
        return Result.ok(uServiceUser);
    }
}

